package cn.maxmc.murderaddons.command

import cn.maxmc.murderaddons.machine.BaseMachine
import cn.maxmc.murderaddons.machine.MachineManager
import io.izzel.taboolib.module.command.base.*
import io.izzel.taboolib.module.locale.TLocale
import org.bukkit.Location
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.permissions.PermissionDefault
import plugily.projects.murdermystery.arena.ArenaRegistry

@BaseCommand(name = "murderaddons",permission = "murderaddons.admin",permissionDefault = PermissionDefault.OP,description = "Murder Addons主命令", aliases = ["ma","muradd","madd"])
class MainCommand: BaseMainCommand() {
    companion object {
        val playerMap = HashMap<Player, ArenaTaskEditor>()
    }

    @SubCommand(type = CommandType.PLAYER, description = "选择进行编辑的地图")
    val selectArena = object : BaseSubCommand() {
        override fun getArguments(): Array<Argument> {
            return arrayOf(Argument("arena") { return@Argument ArenaRegistry.getArenas().map { it.id } })
        }

        override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>) {
            sender as Player
            val arenaName = args[0]
            val arena = ArenaRegistry.getArena(arenaName) ?: return TLocale.sendTo(sender, "player.no_arena")

            playerMap[sender] = ArenaTaskEditor(sender, arena)

            TLocale.sendTo(sender, "player.selected", arena.mapName)
        }
    }

    @SubCommand(type = CommandType.PLAYER, description = "设置一个机器")
    val setupMachine = object : BaseSubCommand() {
        override fun getArguments(): Array<Argument> {
            return arrayOf(
                Argument("machine", true) { return@Argument MachineManager.machines.map { it.simpleName } }
            )
        }

        override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>) {
            sender as Player
            val editor = playerMap[sender] ?: return TLocale.sendTo(sender, "player.not_selected")
            val machineName = args[0]
            val machine = MachineManager.machines.filter { it.simpleName == machineName }[0]

            val constructors =
                machine.declaredConstructors.filter { it.parameterCount != 0 && it.parameterTypes[0] == Location::class.java }
            val constructor = constructors[0]

            val loc = editor.mainLocation ?: return TLocale.sendTo(sender, "player.no_location")

            val machineObj = if (constructor.parameterCount == 1) {
                constructor.newInstance(loc, *editor.locations.toArray()) as BaseMachine
            } else {
                constructor.newInstance(loc, *editor.locations.toArray()) as BaseMachine
            }

            editor.addMachine(machineObj)
            editor.reset()
        }
    }

    @SubCommand(type = CommandType.PLAYER, description = "设置机器主地点", arguments = [])
    val setMainLocation = object : BaseSubCommand() {
        override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>) {
            sender as Player

            val editor = playerMap[sender] ?: return TLocale.sendTo(sender, "player.not_selected")
            val loc = sender.location.block.location
            editor.mainLocation = loc

            TLocale.sendTo(sender, "player.location_set", "x: ${loc.x}, y: ${loc.y}, z: ${loc.z}")
        }
    }

    @SubCommand(type = CommandType.PLAYER, description = "添加机器副地点", arguments = [])
    val addSubLocation = object : BaseSubCommand() {
        override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>) {
            sender as Player

            val editor = playerMap[sender] ?: return TLocale.sendTo(sender, "player.not_selected")
            val loc = sender.location.block.location
            editor.locations.add(loc)

            TLocale.sendTo(sender, "player.location_add", "x: ${loc.x}, y: ${loc.y}, z: ${loc.z}")
        }
    }

    @SubCommand(type = CommandType.PLAYER, description = "保存当前设置", arguments = [])
    val save = object : BaseSubCommand() {
        override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>) {
            sender as Player
            val editor = playerMap[sender] ?: return TLocale.sendTo(sender, "player.not_selected")

            editor.save()
            TLocale.sendTo(sender,"player.save", editor.arena.mapName)

            playerMap.remove(sender)
        }
    }
}